from django.db import models
from django.core.urlresolvers import reverse
from datetime import datetime

#signals: possibilita que se utilize todos os beneficio do signails :)
from django.db.models import signals

#transforma uma string no formato padrao de um slug
from django.template.defaultfilters import slugify

class Artigo(models.Model):
	class Meta:
		ordering = ('-publicacao',)
	publicacao = models.DateTimeField( default = datetime.now, blank = True )	
	titulo = models.CharField(max_length = 100)
	conteudo = models.TextField()	
	resumo = models.TextField(max_length = 5,blank = True)
	slug = models.SlugField(max_length = 100, blank = True)
	
	def get_absolute_url(self):
		return reverse('blog.views.artigo', kwargs = {'slug': self.slug})

def artigo_pre_save(signal, instance, sender, **kwargs):
	instance.slug = slugify(instance.titulo)
	if not len( instance.resumo)!= 0:
		for i in range(len(instance.conteudo)):
			if i > 1000:
				break
			instance.resumo += instance.conteudo[i]
		instance.resumo += "..."
signals.pre_save.connect(artigo_pre_save, sender = Artigo)
